package com.jetsoft.activity.product;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.jetsoft.R;
import com.jetsoft.application.MyApplication;
import com.jetsoft.io.CommunicationServer;
import com.jetsoft.io.ExcuteThread;
import com.jetsoft.utils.StaticValues;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.annotation.view.ViewInject;

public class ProImgGridActivity extends FinalActivity implements OnItemClickListener{
	@ViewInject(id=R.id.pro_img_grid)
	private GridView pro_img_grid;
	
	List<String> picPaths = new LinkedList<String>();
	
	private FinalBitmap fb;
	
	private Handler handler = new Handler();
	
	private MyApplication application;
	
	private String server;
	
	private ImgAdapter im;
	
	@ViewInject(id=R.id.info)
	private TextView info;
	
	@ViewInject(id=R.id.nv_return,click="onClick")
	private Button na_return;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.StyledIndicators);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pro_img_list);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("图片");
		
		application = (MyApplication) getApplication();
		fb = FinalBitmap.create(this);
		fb.configCalculateBitmapSizeWhenDecode(true);
		im = new ImgAdapter();
		pro_img_grid.setAdapter(im);
		pro_img_grid.setOnItemClickListener(this);
		
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("cMode", "Z");
		param.put("szGid", getIntent().getStringExtra("szGid"));
		if(application.getUserEntity()!=null){
			param.put("szOperator", application.getUserEntity().getId());
		}
		
		SharedPreferences preferences = getSharedPreferences(StaticValues.CONFIG, Activity.MODE_PRIVATE);
		server = "http://"+preferences.getString(StaticValues.SERVER_FLAG, getString(R.string.server))+":"
				+ preferences.getString(StaticValues.PORT_FLAG, getString(R.string.port));
		String action = getString(R.string.product_pic_action);
		
		CommunicationServer cs = new CommunicationServer(this, application.getClient(), server+action, param, handler, new GetPics());
		Thread t = new Thread(cs);
		t.start();
	}
	
	class GetPics extends ExcuteThread{
		
		@Override
		public void run() {
			try {
				JSONArray ja = new JSONArray(getJsonString());
				for(int i=0;i<ja.length();i++){
					JSONObject jo = ja.getJSONObject(i);
					String url = jo.getString("url");
					if(url!=null && !"".equals(url)){
						picPaths.add(url);
					}
				}
				if(picPaths.size()>0){
					info.setVisibility(View.GONE);
					im.notifyDataSetChanged();
				}else{
					info.setText("没有图片");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	class ImgAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return picPaths.size();
		}

		@Override
		public Object getItem(int position) {
			return picPaths.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView iv;
		    if(convertView == null){
		        convertView = LayoutInflater.from(ProImgGridActivity.this).inflate(R.layout.image_item, null);
		        iv = (ImageView) convertView.findViewById(R.id.img);
		        iv.setScaleType(ScaleType.CENTER_CROP);
		        convertView.setTag(iv);
		    }else{
		        iv = (ImageView) convertView.getTag();
		    }
		    //bitmap加载就这一行代码，display还有其他重载，详情查看源码
		    String path = server+getString(R.string.pic_path)+picPaths.get(position);
		    fb.display(iv, path);
		    return convertView;
		    }
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Dialog dialog = new Dialog(this,R.style.dialog);
		dialog.setContentView(R.layout.image_show);
		ImageView iv = (ImageView) dialog.findViewById(R.id.img);
		String path = server+getString(R.string.pic_path)+picPaths.get(arg2);
		fb.display(iv, path);
		dialog.show();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
